local hx__renxin = fk.CreateSkill {
  name = "hx__renxin",
}

local spec = {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if target and target ~= player and player:hasSkill(hx__renxin.name) and player:usedSkillTimes(hx__renxin.name, Player.HistoryRound) < 2 then
      return table.find(player:getCardIds("he"), function(id) return Fk:getCardById(id).type == Card.TypeEquip end)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local equipCard = player.room:askToDiscard(player, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = hx__renxin.name,
      pattern = ".|.|.|.|.|equip",
      cancelable = true,
      prompt = "#hx__renxin-invoke::" .. target.id,
      skip = true,
    })
    if #equipCard > 0 then
      local choices = {}
      if player:getMark("@hx__renxin_judge") == 0 then
        table.insert(choices, "hx__judge")
      end
      if player:getMark("@hx__renxin_draw") == 0 then
        table.insert(choices, "hx__draw")
      end
      if player:getMark("@hx__renxin_play") == 0 then
        table.insert(choices, "hx__play")
      end
      if player:getMark("@hx__renxin_discard") == 0 then
        table.insert(choices, "hx__discard")
      end
      if #choices >= 2 then
        local selectedPhases = player.room:askToChoices(player, {
          choices = choices,
          min_num = 2,
          max_num = 2,
          skill_name = hx__renxin.name,
          cancelable = false,
        })
        if selectedPhases then
          event:setCostData(self, { card = equipCard, choice = selectedPhases })
          return true
        end
      end
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local selectedPhases = event:getCostData(self).choice
    local equipCardId = event:getCostData(self).card[1]
    room:throwCard(equipCardId, hx__renxin.name, player, player)
    room:addPlayerMark(player, "@hx__renxin", 1)
    if target.hp < 1 then
      room:recover { num = 1 - target.hp, skillName = hx__renxin.name, who = target }
    end
    for _, phase in ipairs(selectedPhases) do
      if phase == "hx__judge" then
        room:addPlayerMark(player, "@hx__renxin_judge", 1)
      elseif phase == "hx__draw" then
        room:addPlayerMark(player, "@hx__renxin_draw", 1)
      elseif phase == "hx__play" then
        room:addPlayerMark(player, "@hx__renxin_play", 1)
      elseif phase == "hx__discard" then
        room:addPlayerMark(player, "@hx__renxin_discard", 1)
      end
    end
  end,
}

hx__renxin:addEffect(fk.DamageInflicted, spec)
hx__renxin:addEffect(fk.EnterDying, spec)

hx__renxin:addEffect(fk.TurnEnd, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__renxin.name, true) and (player:getMark("@hx__renxin_judge") > 0 or player:getMark("@hx__renxin_draw") > 0 or player:getMark("@hx__renxin_play") > 0 or player:getMark("@hx__renxin_discard") > 0)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:getMark("@hx__renxin_judge") > 0 then
      room:addPlayerMark(player, "hx__renxin_judge_skip", 1)
    end
    if player:getMark("@hx__renxin_draw") > 0 then
      room:addPlayerMark(player, "hx__renxin_draw_skip", 1)
    end
    if player:getMark("@hx__renxin_play") > 0 then
      room:addPlayerMark(player, "hx__renxin_play_skip", 1)
    end
    if player:getMark("@hx__renxin_discard") > 0 then
      room:addPlayerMark(player, "hx__renxin_discard_skip", 1)
    end
  end,
})

hx__renxin:addEffect(fk.EventPhaseChanging, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__renxin.name, true) and
      ((data.phase == Player.Judge and player:getMark("@hx__renxin_judge") > 0 and player:getMark("hx__renxin_judge_skip") > 0) or
      (data.phase == Player.Draw and player:getMark("@hx__renxin_draw") > 0 and player:getMark("hx__renxin_draw_skip") > 0) or
      (data.phase == Player.Play and player:getMark("@hx__renxin_play") > 0 and player:getMark("hx__renxin_play_skip") > 0) or
      (data.phase == Player.Discard and player:getMark("@hx__renxin_discard") > 0 and player:getMark("hx__renxin_discard_skip") > 0) )
    end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:getMark("hx__renxin_judge_skip") > 0 and data.phase == Player.Judge then
      data.skipped = true
      room:setPlayerMark(player, "@hx__renxin_judge", 0)
      room:setPlayerMark(player, "hx__renxin_judge_skip", 0)
    elseif player:getMark("hx__renxin_draw_skip") > 0 and data.phase == Player.Draw then
      data.skipped = true
      room:setPlayerMark(player, "@hx__renxin_draw", 0)
      room:setPlayerMark(player, "hx__renxin_draw_skip", 0)
    elseif player:getMark("hx__renxin_play_skip") > 0 and data.phase == Player.Play then
      data.skipped = true
      room:setPlayerMark(player, "@hx__renxin_play", 0)
      room:setPlayerMark(player, "hx__renxin_play_skip", 0)
    elseif player:getMark("hx__renxin_discard_skip") > 0 and data.phase == Player.Discard then
      data.skipped = true
      room:setPlayerMark(player, "@hx__renxin_discard", 0)
      room:setPlayerMark(player, "hx__renxin_discard_skip", 0)
    end
  end,
})


return hx__renxin
